package com.zg.nacos.util;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zg.common.core.R;
import com.zg.common.core.exception.E;

import java.util.List;
import java.util.Map;

/**
 * <p>Description: </p>
 *
 * @author zhanfeng.liang
 * @since 2025/6/26 14:37
 */

public class NacosUtils {

    public static R genPage(Map nacosResponse,Integer pageNo,Integer pageSize){
        List<Map> serviceList = (List<Map>) nacosResponse.get("pageItems");

        Integer total = (Integer) nacosResponse.get("totalCount");

        // 构建MyBatis-Plus的Page对象
        Page<Map> page = new Page<>(pageNo, pageSize, total);
        page.setRecords(serviceList);
        return R.success(page);
    }


    public static R handleResponse(Map<String,Object> map) {
        if(200==(Integer) map.get("code")){
            return R.success(map.get("data"));
        } else {
            // 从响应中提取错误信息
            E.ERROR.happen();
            return null;
        }
    }

    public static R handleResponse(String res) {
        if (res.equals("true")) {
            return R.success();
        } else {
            // 从响应中提取错误信息
             E.ERROR.happen();
        }
        return null;
    }


}
